package com.resume.controller

import com.resume.common.Result
import com.resume.dto.CreateResumeRequest
import com.resume.dto.GenerateResumeRequest
import com.resume.service.ResumeService
import org.springframework.http.ResponseEntity
import org.springframework.security.core.Authentication
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import jakarta.validation.Valid

@RestController
@RequestMapping("/api/resumes")
@CrossOrigin(origins = ["http://localhost:3000"])
class ResumeController(
    private val resumeService: ResumeService
) {
    
    @GetMapping
    fun getMyResumes(
        @RequestParam(defaultValue = "1") page: Int,
        @RequestParam(defaultValue = "10") size: Int,
        authentication: Authentication?
    ): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            val result = resumeService.getResumesByUserId(userId, page, size)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "获取简历列表失败")
        }
    }
    
    @GetMapping("/{id}")
    fun getResume(@PathVariable id: Long, authentication: Authentication?): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            val result = resumeService.getResume(id, userId)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "获取简历失败")
        }
    }
    
    @PostMapping
    fun createResume(@Valid @RequestBody request: CreateResumeRequest, authentication: Authentication?): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            val result = resumeService.createResume(request, userId)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "创建简历失败")
        }
    }
    
    @PutMapping("/{id}")
    fun updateResume(
        @PathVariable id: Long,
        @Valid @RequestBody request: CreateResumeRequest,
        authentication: Authentication?
    ): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            val result = resumeService.updateResume(id, request, userId)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "更新简历失败")
        }
    }
    
    @DeleteMapping("/{id}")
    fun deleteResume(@PathVariable id: Long, authentication: Authentication?): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            resumeService.deleteResume(id, userId)
            Result.success("删除成功")
        } catch (e: Exception) {
            Result.error(e.message ?: "删除简历失败")
        }
    }
    
    @PostMapping("/generate")
    fun generateResume(@Valid @RequestBody request: GenerateResumeRequest, authentication: Authentication?): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            val result = resumeService.generateResumeByPrompt(request, userId)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "生成简历失败")
        }
    }
    
    @PostMapping("/upload-template")
    fun uploadTemplate(
        @RequestParam("file") file: MultipartFile,
        authentication: Authentication?
    ): Result<Any> {
        return try {
            val userId = getUserId(authentication)
            val result = resumeService.generateResumeFromTemplate(file, userId)
            Result.success(result)
        } catch (e: Exception) {
            Result.error(e.message ?: "上传模板失败")
        }
    }
    
    @PostMapping("/{id}/export")
    fun exportResume(
        @PathVariable id: Long,
        @RequestParam format: String,
        authentication: Authentication?
    ): ResponseEntity<ByteArray> {
        return try {
            val userId = getUserId(authentication)
            resumeService.exportResume(id, format, userId)
        } catch (e: Exception) {
            ResponseEntity.badRequest().build()
        }
    }
    
    private fun getUserId(authentication: Authentication?): Long {
        return if (authentication != null) {
            authentication.name.toLong()
        } else {
            // 临时使用默认用户ID 1
            1L
        }
    }
} 